#!/usr/bin/env python3
# 将bin2wave转换得到的波形数据进一步处理，整理出满足权重优化脚本要求的格式

import os, sys
from tqdm import trange
from pylab import *

WAVE_PATH='waves-10-29'

IMAX = 16
JMAX = 2
NROW = 200
SAMPLES_PER_REG = 20

# idx_offset = SAMPLES_PER_REG*(1179+10)  # 指定挑选波形的位置，overscan
idx_offset = SAMPLES_PER_REG*(5)  # 指定挑选波形的位置，overscan
idx0 = idx_offset + 4
idx1 = idx0 + SAMPLES_PER_REG*60


# print('idx0 = ', idx0, 'idx1 = ', idx1)
# sys.exit(0)


for i in trange(IMAX):
    print('> extracting OS: {}'.format(i+1))
    outdir = 'OS_' + str(i+1)
    if os.path.isdir(outdir) is False:
        os.system('mkdir '+outdir)
    
    for j in range(JMAX):
        d = loadtxt( WAVE_PATH + '/OS_' + str(1+i) + '_' + str(1+j) + '.txt')
        dd = d[idx0:idx1,:]
        for k in range(NROW):    # 只提取NROW行的数据
            fname = outdir + '/' + str(k+1) + '_' + str(j+1) + '.txt'
            savetxt(fname, dd[:,k], fmt='%6d')

    # if i == 0:
    #     sys.exit(0)

